package edu.ucsc.mit2008.swan.core.internal.menu;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.jface.action.Action;

/**
 * Extention class for the editor menus.
 * 
 * @author Thushara Sanjeewa Ramasinghe
 *
 */
public class EditorMenuExtension {
	
	private static final String MENU_ID =  "id";
	private static final String MENU_NAME = "name";
	private static final String MENU_ITEM = "menuItem";
	private static final String MENU_ITEM_NAME = "name";
	private static final String MENU_ITEM_ACTION_CLASS = "actionClass";

	/** The configurable element*/
	private IConfigurationElement element;

	/** The menu id*/
	private String menuId;	
	/** The menu name*/
	private String menuName;
	
	List<EditorMenuItem> menuItems = new ArrayList<EditorMenuExtension.EditorMenuItem>();
	
	/**
	 * Default constructor.
	 * 
	 * @param element
	 */
	public EditorMenuExtension(IConfigurationElement element) {
		this.element = element;
		
		menuId = element.getAttribute(MENU_ID);
		menuName = element.getAttribute(MENU_NAME);
		
		for (IConfigurationElement tabElem : element.getChildren(MENU_ITEM)) {
			EditorMenuItem item = new EditorMenuItem();
			item.setName(tabElem.getAttribute(MENU_ITEM_NAME));
			try {
				item.setAction((Action) tabElem.createExecutableExtension(MENU_ITEM_ACTION_CLASS));
			} catch (CoreException e) {
				e.printStackTrace();
			}
			menuItems.add(item);
		}
	}

	/**
	 * Returns the menu Id.
	 * 
	 * @return
	 */
	public String getMenuId() {
		return menuId;
	}

	/**
	 * Returns the name of the menu.
	 * 
	 * @return
	 */
	public String getMenuName() {
		return menuName;
	}

	public List<EditorMenuItem> getMenuItems(){
		return menuItems;
	}
	
	/**
	 * The menu item.
	 * 
	 * @author Thushara Sanjeewa Ramasinghe
	 *
	 */
	public class EditorMenuItem {
		private String id;
		private String name;
		private String actionClassName;
		private Action action;
		
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getActionClassName() {
			return actionClassName;
		}
		public void setActionClassName(String actionClassName) {
			this.actionClassName = actionClassName;
		}
		public Action getAction() {
			return action;
		}
		public void setAction(Action action) {
			this.action = action;
		}
		
	}
}
